import { _decorator, Button } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { GameEndTzWin_Ctr } from './GameEndTzWin_Ctr';
import { GameType, Respones} from '../../Config/GameCfg';
import { GameTip } from '../../../../Framework/Scripts/Managers/TipMgr';
import { IReward } from '../../../AssetPackage/Props/Game/DT_TZ';
import { ViewMgr } from '../../../../Framework/Scripts/Managers/ViewMgr';
import { IFuHuoRes } from '../proto/gameTz';
import { SceneMgr } from '../../../../Framework/Scripts/Managers/SceneMgr';
const { ccclass, property } = _decorator;

/**
 * 每日挑战复活界面
 */
@ccclass('GameEndTzLose_Ctr')
export class GameEndTzLose_Ctr extends UIComponent {
    reward: IReward;

    isClick: boolean = true;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.AddButtonListener('root/btn1',this,this.OnBtnFHClick);
        this.AddButtonListener('root/btn2',this,this.OnBtn2Click);
    }

    private InitData(){
   
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgStartAnswerTzFuHuoRes,this.onStartAnswerTzFuHuoRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgStartAnswerTzFuHuoRes,this.onStartAnswerTzFuHuoRes,this);
    }

    //点击复活
    private OnBtnFHClick(btn: Button) {
        if(!this.isClick){
            return;
        }
        this.isClick = false

        //这里具体是先看完广告，完毕后再发送
        globalThis.gameMgr._GameTzMsg.StartAnswerTzFuHuoReq();
    }

    //点击领取奖励
    private OnBtn2Click(btn:Button) {
        //判断是否有奖励
        if(this.reward.rewardNum > 0){
            let com = UIMgr.inst.ShowUIView(AbNames.GamePlay,'Prefabs/GameTZ/GameEndTzWin',UIType.UnFull) as GameEndTzWin_Ctr;
            com.setDate(this.reward);
        }else{
            this.node.destroy();
            //链接大厅的网络
            const callFunction = SceneMgr.inst.SetCallFun(SceneMgr.inst.showHallViwe);
            let call ={
                func: callFunction,
                path: "Prefabs/GameHall"
            }
            ViewMgr.inst.ConnectToHall(call);
        }
    }

    //复活
    private onStartAnswerTzFuHuoRes(data: IFuHuoRes){
        console.log('==========onStartAnswerTzFuHuoRes=============',data.code);
        if(this.hasProInObj(data,'code')){
            this.isClick = true;
            if(data.code === Respones.OK){
                this.node.destroy();
            }else if(data.code == Respones.TZ_FUOHUO_ERR){
                GameTip.inst.showTip('当前挑战复活次数使用完毕！');
            }
        }
    }

    public setDate(data:any){
        this.reward = data;
    }
}
